#include <iostream>
#include <vector>
#include "lib/list.h"
using namespace std;

void deleteRepeat(ListNode** phead) {
  if (phead == nullptr || *phead == nullptr) {
    return;
  }

  ListNode* pre_node = nullptr;
  ListNode* cur_node = *phead;
  while (cur_node) {
    auto next_node = cur_node->next;
    bool delete_cur_node = false;
    if (next_node && next_node->value == cur_node->value) {
      delete_cur_node = true;
    }

    if (!delete_cur_node) {
      pre_node = cur_node;
      cur_node = next_node;
    } else {
      int val = cur_node->value;
      auto deleted_node = cur_node;
      while (deleted_node && deleted_node->value == val) {
        next_node = deleted_node->next;
        delete deleted_node;
        deleted_node = next_node;
      }
      if (pre_node == nullptr) {
        *phead = next_node;
      } else {
        pre_node->next = next_node;
      }
      cur_node = next_node;
    }
  }
}

int main() {
  vector<int> node_vals = {1, 1, 2, 3, 3, 3, 4, 4, 5, 6, 6};
  ListNode* phead = CreateList(node_vals);
  deleteRepeat(&phead);
  PrintList(phead);
  return 0;
}
